package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.sys.entity.Dept;
import com.dataseek.iot.sys.entity.UserDept;
import com.dataseek.iot.sys.mapper.DeptMapper;
import com.dataseek.iot.sys.mapper.UserDeptMapper;
import com.dataseek.iot.sys.service.DeptService;
import com.dataseek.iot.sys.vo.DeptVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolkit.auth.entity.LoginUser;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserDeptMapper userDeptMapper;

    @Override
    public DeptVo queryDept(String deptName, String status, Integer num, Integer size) {

        DeptVo deptVo = new DeptVo();
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        if (deptName != null && !deptName.isEmpty()) {
            qw.like("dept_name", deptName);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq("status", status);
        }
        IPage<Dept> page = new Page<>(num, size);
        List<Dept> mapList = deptMapper.selectPage(page, qw).getRecords();
        deptVo.setPage(num);
        deptVo.setSize(size);
        deptVo.setTotal(page.getTotal());
        deptVo.setList(mapList);
        return deptVo;
    }

    //新增部门信息
    @Override
    public Integer insertDept(Dept dept) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        int resultCode = 0;
        String code = dept.getDeptName();
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("dept_name", code);
        Integer count = deptMapper.selectCount(qw);
        if (count == 0) {
            dept.setTenantCode(tenantCode);
            this.saveOrUpdate(dept);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateDept(Dept dept) {
        int resultCode = deptMapper.updateById(dept);
        return resultCode;
    }

    @Override
    public Integer deleteDept(List<Integer> tenantIds) {
        Integer resultCode = 0;
        Integer count = 0;
        for (int i = 0; i < tenantIds.size(); i++) {
            Integer id = tenantIds.get(i);
            QueryWrapper<UserDept> qw = new QueryWrapper<>();
            qw.eq("dept_id", id);
           count = count + userDeptMapper.selectCount(qw);
        }
        if(count> 0 ){
            resultCode = RspStatusEnum.FAIL.getCode();
        } else {
            resultCode = deptMapper.deleteBatchIds(tenantIds);
        }
        return resultCode;
    }

}
